using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using OVS.BusinessObjects;

namespace OVS.DAL
{
	public partial class Image_DetailsDAO
	{
		public Image_DetailsDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<Image_Details> GetImage_Detailss()
		{
			try
			{
				List<Image_Details> lstImage_Detailss = new List<Image_Details>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [imde_id],[imha_id],[imde_sizeH],[imde_sizeL] FROM [Image_Details]",CommandType.Text);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					Image_Details oImage_Details = new Image_Details();
					oImage_Details.imde_id = Convert.ToInt32(oDbDataReader["imde_id"]);
					oImage_Details.imha_id = Convert.ToInt32(oDbDataReader["imha_id"]);

					if(oDbDataReader["imde_sizeH"] != DBNull.Value)
						oImage_Details.imde_sizeH = Convert.ToDouble(oDbDataReader["imde_sizeH"]);

					if(oDbDataReader["imde_sizeL"] != DBNull.Value)
						oImage_Details.imde_sizeL = Convert.ToDouble(oDbDataReader["imde_sizeL"]);
					lstImage_Detailss.Add(oImage_Details);
				}
				oDbDataReader.Close();
				return lstImage_Detailss;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public Image_Details GetImage_Details(int imde_id)
		{
			try
			{
				Image_Details oImage_Details = new Image_Details();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [imde_id],[imha_id],[imde_sizeH],[imde_sizeL] FROM [Image_Details] WHERE [imde_id]=@imde_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_id",DbType.Int32,imde_id));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oImage_Details.imde_id = Convert.ToInt32(oDbDataReader["imde_id"]);
					oImage_Details.imha_id = Convert.ToInt32(oDbDataReader["imha_id"]);

					if(oDbDataReader["imde_sizeH"] != DBNull.Value)
						oImage_Details.imde_sizeH = Convert.ToDouble(oDbDataReader["imde_sizeH"]);

					if(oDbDataReader["imde_sizeL"] != DBNull.Value)
						oImage_Details.imde_sizeL = Convert.ToDouble(oDbDataReader["imde_sizeL"]);
				}
				oDbDataReader.Close();
				return oImage_Details;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddImage_Details(Image_Details oImage_Details)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERT INTO [Image_Details]([imha_id],[imde_sizeH],[imde_sizeL])VALUES(@imha_id,@imde_sizeH,@imde_sizeL);SELECT SCOPE_IDENTITY();",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imha_id",DbType.Int32,oImage_Details.imha_id));
				if (oImage_Details.imde_sizeH.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_sizeH",DbType.Double,oImage_Details.imde_sizeH));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_sizeH",DbType.Double,DBNull.Value));
				if (oImage_Details.imde_sizeL.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_sizeL",DbType.Double,oImage_Details.imde_sizeL));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_sizeL",DbType.Double,DBNull.Value));

				return Convert.ToInt32(DbProviderHelper.ExecuteScalar(oDbCommand));
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateImage_Details(Image_Details oImage_Details)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [Image_Details] SET [imha_id]=@imha_id,[imde_sizeH]=@imde_sizeH,[imde_sizeL]=@imde_sizeL WHERE [imde_id]=@imde_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imha_id",DbType.Int32,oImage_Details.imha_id));
				if (oImage_Details.imde_sizeH.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_sizeH",DbType.Double,oImage_Details.imde_sizeH));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_sizeH",DbType.Double,DBNull.Value));
				if (oImage_Details.imde_sizeL.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_sizeL",DbType.Double,oImage_Details.imde_sizeL));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_sizeL",DbType.Double,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_id",DbType.Int32,oImage_Details.imde_id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveImage_Details(Image_Details oImage_Details)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [Image_Details] WHERE [imde_id]=@imde_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_id",DbType.Int32,oImage_Details.imde_id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveImage_Details(int imde_id)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [Image_Details] WHERE [imde_id]=@imde_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imde_id",DbType.Int32,imde_id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public List<Image_Details> GetImage_DetailssOfImage_Haar(int imha_id)
		{
			try
			{
				List<Image_Details> lstImage_Detailss = new List<Image_Details>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [imde_id],[imha_id],[imde_sizeH],[imde_sizeL] FROM [Image_Details] WHERE [imha_id]=@imha_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imha_id",DbType.Int32,imha_id));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					Image_Details oImage_Details = new Image_Details();
					oImage_Details.imde_id = Convert.ToInt32(oDbDataReader["imde_id"]);
					oImage_Details.imha_id = Convert.ToInt32(oDbDataReader["imha_id"]);

					if(oDbDataReader["imde_sizeH"] != DBNull.Value)
						oImage_Details.imde_sizeH = Convert.ToDouble(oDbDataReader["imde_sizeH"]);

					if(oDbDataReader["imde_sizeL"] != DBNull.Value)
						oImage_Details.imde_sizeL = Convert.ToDouble(oDbDataReader["imde_sizeL"]);
					lstImage_Detailss.Add(oImage_Details);
				}
				oDbDataReader.Close();
				return lstImage_Detailss;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public Image_Haar GetImage_HaarOfImage_Details(int imha_id)
		{
			try
			{
				Image_Haar oImage_Haar = new Image_Haar();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [imha_id],[imha_fn],[imha_label],[imha_height],[imha_width],[imha_desc],[imha_scaleFactor],[imha_minNeighbors] FROM [Image_Haar] WHERE [imha_id]=@imha_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imha_id",DbType.Int32,imha_id));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oImage_Haar.imha_id = Convert.ToInt32(oDbDataReader["imha_id"]);
					oImage_Haar.imha_fn = Convert.ToString(oDbDataReader["imha_fn"]);

					if(oDbDataReader["imha_label"] != DBNull.Value)
						oImage_Haar.imha_label = Convert.ToString(oDbDataReader["imha_label"]);

					if(oDbDataReader["imha_height"] != DBNull.Value)
						oImage_Haar.imha_height = Convert.ToInt32(oDbDataReader["imha_height"]);

					if(oDbDataReader["imha_width"] != DBNull.Value)
						oImage_Haar.imha_width = Convert.ToInt32(oDbDataReader["imha_width"]);

					if(oDbDataReader["imha_desc"] != DBNull.Value)
						oImage_Haar.imha_desc = Convert.ToString(oDbDataReader["imha_desc"]);

					if(oDbDataReader["imha_scaleFactor"] != DBNull.Value)
						oImage_Haar.imha_scaleFactor = Convert.ToDouble(oDbDataReader["imha_scaleFactor"]);

					if(oDbDataReader["imha_minNeighbors"] != DBNull.Value)
						oImage_Haar.imha_minNeighbors = Convert.ToDouble(oDbDataReader["imha_minNeighbors"]);
				}
				oDbDataReader.Close();
				return oImage_Haar;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
